package com.smarth.zhizulian.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.adapter.SharePagerAdapter2;
import com.smarth.zhizulian.app.BaseActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.entity.UserInfo;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.popupwindow.ShareMeDialog;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.MoneyUtils;
import com.smarth.zhizulian.utils.ShareUtils;
import com.smarth.zhizulian.view.viewpager.ZoomOutPageTransformer;
import com.rd.PageIndicatorView;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * Created by 18865 on 2017/12/29.
 */

public class ShareMeActivity extends BaseActivity {
    public static final String INTENT_USERINFO = "INTENT_USERINFO";
    public static final String INTENT_MODE = "INTENT_MODE";
    public static final int MODE_NORMAL = 0;
    public static final int MODE_MISSION = 1;
    private int mMode;
    @BindView(R.id.pi_indicator)
    PageIndicatorView piIndicator;
    private UserInfo mUserInfo;
    //    private int[] backgrounds = new int[]{R.drawable.bg_shareme2, R.drawable.bg_shareme1, R.drawable.bg_shareme3};
//    private int[] colors = new int[]{R.color.shareme_2, R.color.shareme_1, R.color.shareme_3};
    private int[] layouts = new int[]{R.layout.item_shareme1, R.layout.item_shareme2, R.layout.item_shareme3};

    @BindView(R.id.vp_viewpager)
    ViewPager vpViewpager;
    private List<View> views = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shareme);
        setTitlebar("晒收入邀好友", true);
        ButterKnife.bind(this);
        mUserInfo = (UserInfo) getIntent().getSerializableExtra(INTENT_USERINFO);
        mMode = getIntent().getIntExtra(INTENT_MODE, MODE_NORMAL);
        initViews();
        initViewPager();
    }

    private void initViews() {
        for (int i = 0; i < layouts.length; i++) {
            View view = LayoutInflater.from(mContext).inflate(layouts[i], null);
            ImageView iv_avatar = view.findViewById(R.id.iv_avatar);
            ImageView iv_background = view.findViewById(R.id.iv_background);
            TextView tv_money = view.findViewById(R.id.tv_money);
            TextView tv_moneyunit = view.findViewById(R.id.tv_moneyunit);
            TextView tv_name = view.findViewById(R.id.tv_name);
            ImageView iv_qrcode = view.findViewById(R.id.iv_qrcode);
            GlideUtils.loadAvatar(mContext, mUserInfo.getAvatar(), iv_avatar);
            tv_money.setText(MoneyUtils.coin2Money(mUserInfo.getCoin()));
            tv_name.setText(mUserInfo.getName());
//            iv_background.setImageResource(backgrounds[i]);
            GlideUtils.loadNews(mContext, mUserInfo.getQrcode(), iv_qrcode);
//            tv_money.setTextColor(mContext.getResources().getColor(colors[i]));
//            tv_moneyunit.setTextColor(mContext.getResources().getColor(colors[i]));
            views.add(view);
        }
    }

    private void initViewPager() {
        vpViewpager.setOffscreenPageLimit(3);
        vpViewpager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.ipx_79));
        vpViewpager.setPageTransformer(true, new ZoomOutPageTransformer());
//        vpViewpager.setAdapter(new SharePagerAdapter(mContext, mUserInfo, new int[]{R.drawable.bg_shareme2, R.drawable.bg_shareme1, R.drawable.bg_shareme3}, new int[]{R.color.shareme_2, R.color.shareme_1, R.color.shareme_3}));
        vpViewpager.setAdapter(new SharePagerAdapter2(mContext, mUserInfo, views));
        piIndicator.setViewPager(vpViewpager);
        int select = new Random().nextInt(3);
        KumaLog.kuma("select:" + select);
        piIndicator.setSelected(select);
        vpViewpager.setCurrentItem(select);
    }

    public static void open(Context context, UserInfo ui) {
        Intent i = new Intent(context, ShareMeActivity.class);
        i.putExtra(INTENT_USERINFO, ui);
        context.startActivity(i);
    }

    public static void openForMission(Context context, UserInfo ui) {
        Intent i = new Intent(context, ShareMeActivity.class);
        i.putExtra(INTENT_USERINFO, ui);
        i.putExtra(INTENT_MODE, MODE_MISSION);
        context.startActivity(i);
    }

    @OnClick({R.id.btn_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_share:
                ShareMeDialog dialog = new ShareMeDialog(mContext, mMode == MODE_NORMAL);
                dialog.setOnClickMomentListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        View v = views.get(vpViewpager.getCurrentItem());
                        v.setDrawingCacheEnabled(true);
                        v.buildDrawingCache();  //启用DrawingCache并创建位图
                        Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
                        v.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
                        ShareUtils.shareToMomentImage("【直租链】分享就能赚钱，每天签到赚钱倍增，天天都有幸运红包一元就可提现，收徒更有30%奖励提成，现在下载注册最高可送66元现金，亲测绝对真实哦", bitmap, new PlatformActionListener() {
                            @Override
                            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                KumaToast.show(mContext, "分享成功");
                                KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHOWMONEY)
                                        .compose(RxCompos.<Long>getBEAllLoginCompos())
                                        .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext) {
                                            @Override
                                            public void onNext(BaseEntity<Long> o) {
                                                EventBus.getDefault().post(new EventBean("ShareMeActivity"), BaseConfig.EventTag.ON_FINISHMISSION);
                                            }
                                        });
                            }

                            @Override
                            public void onError(Platform platform, int i, Throwable throwable) {
                                KumaToast.show(mContext, "分享失败");
                            }

                            @Override
                            public void onCancel(Platform platform, int i) {
                                KumaToast.show(mContext, "取消分享");
                            }
                        });
                    }
                });
                dialog.setOnClickWechatListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        View v = views.get(vpViewpager.getCurrentItem());
                        v.setDrawingCacheEnabled(true);
                        v.buildDrawingCache();  //启用DrawingCache并创建位图
                        Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
                        v.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
                        ShareUtils.shareToWechatImage(bitmap, new PlatformActionListener() {
                            @Override
                            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                KumaToast.show(mContext, "分享成功");
                                KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHOWMONEY)
                                        .compose(RxCompos.<Long>getBEAllLoginCompos())
                                        .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext) {
                                            @Override
                                            public void onNext(BaseEntity<Long> o) {
                                                EventBus.getDefault().post(new EventBean("ShareMeActivity"), BaseConfig.EventTag.ON_FINISHMISSION);
                                            }
                                        });
                            }

                            @Override
                            public void onError(Platform platform, int i, Throwable throwable) {
                                KumaToast.show(mContext, "分享失败");
                            }

                            @Override
                            public void onCancel(Platform platform, int i) {
                                KumaToast.show(mContext, "取消分享");
                            }
                        });
                    }
                });
                dialog.setOnClickQQListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        View v = views.get(vpViewpager.getCurrentItem());
                        v.setDrawingCacheEnabled(true);
                        v.buildDrawingCache();  //启用DrawingCache并创建位图
                        Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
                        v.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
                        ShareUtils.shareToQQImage(mContext, bitmap, new PlatformActionListener() {
                            @Override
                            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                KumaToast.show(mContext, "分享成功");
                                KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHOWMONEY)
                                        .compose(RxCompos.<Long>getBEAllLoginCompos())
                                        .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext) {
                                            @Override
                                            public void onNext(BaseEntity<Long> o) {
                                                EventBus.getDefault().post(new EventBean("ShareMeActivity"), BaseConfig.EventTag.ON_FINISHMISSION);
                                            }
                                        });
                            }

                            @Override
                            public void onError(Platform platform, int i, Throwable throwable) {
                                KumaToast.show(mContext, "分享失败");
                            }

                            @Override
                            public void onCancel(Platform platform, int i) {
                                KumaToast.show(mContext, "取消分享");
                            }
                        });
                    }
                });
                dialog.show();

                break;
        }
    }
}
